var person = {}
Object.defineProperty(person, 'name', {
  writable: false,
  value: 'lee'
})
console.log(person.name) // lee
person.name = 'mon'
console.log(person.name) // lee   由于writable设置未False,此处并未改变name的值







var child = {}
Object.defineProperty(child, 'name', {
  configurable: false, // 一旦设置为False,  今后设置除writable之外的其他属性，就会报错
  value: 'd'
})
console.log(child.name) //d
delete child.name
console.log(child.name) //d  由于设置了configurable为false，并未删除成功

try {
  Object.defineProperty(child, 'name', {
    configurable: true,
    value: 'd'
  })
} catch (e) {
  console.log(e) // 抛出了错误
}
try {
  Object.defineProperty(child, 'name', {
    configurable: false,
    value: 'c'
  })
} catch (e) {
  console.log(e) // 抛出了错误
}

try {
  Object.defineProperty(child, 'name', {
    writable: true // 设置为true会抛出错误， False额时候不会
  })
} catch (e) {
  console.log(e) // 抛出了错误
}






// Object.defineProperty创建属性时候的默认值

var desk = {}
Object.defineProperty(desk, 'name', {
  value: '222'
})
try {
  Object.defineProperty(desk, 'name', {
    value: '3333'
  })
} catch (e) {
  console.log(e) // 抛出错误，connot redefined....
  // 因为在第一次定义的时候，configurable的默认值为false了
}


// 访问器属性
var book = {
  _year: 2004,
  editor: 1
}
Object.defineProperty(book, 'year', { // 定义了第三个属性
  get: function () {
    return this._year
  },
  set: function (newValue) {
    this._year = newValue
    this.editor = newValue - 2004
  }
})

book.year = 2005
console.log(book.editor) // 1
console.log(book._year) // 2005
console.log(book.year)// 2005

// Object.defineProperties()支持同时定义一个对象的多个属性
var desk = {}
Object.defineProperties(desk, {
  _year: {
    writable: true,
    value: 2004
  },
  editor: {
    writable: true,
    value: 1
  },
  year: {
    get: function() {
      return this._year
    },
    set: function (newValue) {
      if (newValue > 2004) {
        this._year = newValue
        this.editor = newValue - 2004
      }
    }
  }
})
// 读取属性的特性  Object.getOwnPropertyDescriptor()
var descriptor = Object.getOwnPropertyDescriptor(desk, '_year')
console.log(descriptor.value) // 2004
console.log(descriptor.configurable) // false
console.log(descriptor.get) // undefined

var descriptor = Object.getOwnPropertyDescriptor(desk, 'year')
console.log(descriptor.value) // undefined  这时候是访问器属性，并不会调用get方法
console.log(descriptor.enumerable) // false
console.log(typeof descriptor.get) // function



// 字面量方式创建的对象属性，默认访问器属性都是true

// 直接通过Object.define创建的属性，都是False

